<?php
// $Id: variable.drush.inc,v 1.19 2010/03/25 02:48:17 weitzman Exp $

/**
 * Implementation of hook_drush_help().
 *
 * This function is called whenever a drush user calls
 * 'drush help <name-of-your-command>'
 *
 * @param
 *   A string with the help section (prepend with 'drush:')
 *
 * @return
 *   A string with the help text for your command.
 */
function cforge_drush_help($section) {
  switch ($section) {
    case 'drush:cforge-refresh':
      return dt("Refresh one of translations, permissions, views, blocks, menus, or all");
  }
}


/**
 * Implementation of hook_drush_command().
 *
 * In this hook, you specify which commands your
 * drush module makes available, what it does and
 * description.
 *
 * Notice how this structure closely resembles how
 * you define menu hooks.
 *
 * @return
 *   An associative array describing your command(s).
 */
function cforge_drush_command() {
  $items['cforge-reset'] = array(
    'callback' => 'drush_cforge_reset',
    'description' => 'Reset certain elements of the cforge site. See profiles/cforge/modules/cforge_custom/cforge_custom.inc for details',
    'arguments' => array(
      'type' => 'translations, permissions, views, blocks, menus, or all',
      'param' => '(optional) depends on above, or all',
    ),
    'examples' => array(
      'drush cforge-reset permissions 3' => 'reset permissions for trader role',
      'drush cforge-reset views member_list --hard' => 'reset all the menus provided by cforge_custom module, even if they have been altered',
      'drush cforge-reset blocks left' => 'replace all the blocks in the left region',
      'drush cforge-reset translations' => 'reload all the translations from /profiles/cfroge/translations in the sites default language'
    ),
    'options' => array(
        '--hard' => 'hard reset'
    ),
    'aliases' => array(
    //  'cforge-reset'
    ),
  );
  $items['cforge-report'] = array(
    'callback' => 'drush_cforge_report',
    'description' => 'show the standard community participation stats',
  );
  $items['cforge-mail'] = array(
    'callback' => 'drush_cforge_contact_role',
    'description' => 'print the email addresses of all the members, by role rid',
    'arguments' => array(
      'rid' => 'the role id of the members you want to list',
    ),
    'examples' => array(
      'drush cforge-mail 4' => 'print the email addresses of all committee members',
    ),
  );

  return $items;
}

function drush_cforge_reset($type, $param = NULL) {
  drush_print(cforge_reset($type, $param, drush_get_option('hard')));
  if (count(batch_get())) drush_backend_batch_process();
}

function drush_cforge_report() {
  module_load_include('inc', 'cforge_custom');
  $stats = cforge_get_report();
  unset($stats['logo']);
  foreach ($stats as $stat) {
    $rows[$stat['title']] = strip_tags($stat['value']);
  }
  drush_print_table(drush_key_value_to_array_table($rows));
}

function drush_cforge_contact_role($rid) {
  $result = db_query("SELECT mail from {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid WHERE ur.rid = %d", $rid);
  while ($mail = db_result($result)) $output .= $mail .', ';
  drush_print($output);
}
